
# """sqlalchemy 操作oracle数据库示例"""

from sqlalchemy import create_engine, Table, MetaData
from sqlalchemy.orm import Session

metadata = MetaData()
# echo 参数用于标记是否输出日志信息
engine = create_engine( 'mysql+pymysql://root:new_password@127.0.0.1:3306/students', echo = True)
session = Session(engine)

# 获取指定数据库表对象
studnet_table = Table('student', metadata, autoload=True, autoload_with=engine)

# # 插入数据
# ret = session.execute(studnet_table.insert(), {"s_id": "09" ,"s_name" :"布鲁斯", "s_birth":"2020-12-09", "s_sex":"女"})
# session.commit()

# 查询数据返回第一条
res = session.query(studnet_table).first()
print(res.s_id)
print(res.s_name)
print(res.s_birth)
print(res.s_sex)

print(studnet_table.c.s_id)

# session.query(studnet_table).filter(studnet_table.c.s_id == "09").update({"s_name": "更新"})

# session.execute(studnet_table.insert(),{"tname":"angel"})#增加新数据

session.execute(studnet_table.delete().where(studnet_table.c.s_id=="09"))#删除数据
session.commit()

# session.execute(studnet_table.update().where(studnet_table.c.s_id == "09").values(s_name="update"))#修改数据
# session.commit()

# session.execute("update student set s_name = :sname where s_id = :sid", params={"sname":"update", "sid":"09"})#修改数据

# teahcer_obj1=session.execute(studnet_table.select().where(studnet_table.c.tid>1).where(teacher_1.c.tid<10))#查询数据

# teahcer_obj = session.query(studnet_table).filter_by(s_id="").all()#查询数据

# if __name__ == '__main__':
#     pass